<template>
  <!-- toast -->
  <div v-if="showToastE" class="toast-box" @click="close">
    <div class="toast animate__animated animate__fadeIn">
      {{ toastMsg }}
    </div>
  </div>
</template>

<script>
export default {
  name: 'MyToast',
  props: {
    msg: String,
  },
  data() {
    return {
      showToastE: false,
      toastMsg: '',
      timer: null,
    };
  },
  mounted() {},
  beforeDestroy() {
    this.clearTimer();
  },
  methods: {
    showToast(msg) {
      this.toastMsg = msg;
      this.showToastE = true;
      this.clearTimer();
      this.timer = setTimeout(() => {
        this.showToastE = false;
      }, 2000);
    },
    close() {
      this.clearTimer();
      this.showToastE = false;
    },
    clearTimer() {
      if (this.timer) {
        clearTimeout(this.timer);
        this.timer = null;
      }
    },
  },
};
</script>

<style scoped>
* {
  box-sizing: border-box;
}

.toast-box {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: 20%;
}
.toast {
  padding: 16px 32px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.6);
  font-size: 16px;
  color: #ffffff;
}
</style>
